import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * ATM入口类
 */
public class ATMSystem {
    public static void main(String[] args) {
        //1.定义账户类
        //2.定义集合，装账户
        ArrayList<Account> accounts = new ArrayList<>();
        // 3. 展示系统首页: 开户和登录
        Scanner sc = new Scanner(System.in);
        OUT:
        while (true) {
            System.out.println("==========黑马ATM系统=========");
            System.out.println("1.账户登录");
            System.out.println("2.账户开户");
            System.out.println("3.退出系统");
            System.out.println("输入命令操作:");
            int operation = sc.nextInt();
            switch (operation) {
                case 1:
                    //用户登录
                    login(accounts, sc);
                    break;
                case 2:
                    openAccount(accounts, sc);
                    //用户账户开户
                    break;
                case 3:
                    //退出系统账户
                    break OUT;
                default:
                    System.out.println("输入命令有误,请重新输入!");
            }
        }
    }

    public static String generateCarId(ArrayList<Account> accounts) {
        Random random = new Random();
        while (true) {
            String carId = "";
            for (int i = 0; i < 8; i++) {
                int index = random.nextInt(10);
                carId += index;
            }
            Account account = searchAccount(accounts, carId);
            if (account == null) {
                //说明我们的carId可用
                return carId;
            }
        }
    }

    public static Account searchAccount(ArrayList<Account> accounts, String carId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if (carId.equals(account.getCarId())) {
                return account;
            }
        }
        return null;
    }

    public static void openAccount(ArrayList<Account> accounts, Scanner sc) {
        Account account = new Account();
        System.out.println("=======账户开户========");
        System.out.println("请输入用户名:");
        String userName = sc.next();
        System.out.println("请输入密码:");
        String passWord = sc.next();
        System.out.println("请输入确认密码:");
        String passWord_ok = sc.next();
        while (true) {
            if (!(passWord.equals(passWord_ok))) {
                System.out.println("两次输入密码不一致,请重新输入密码:");
                passWord = sc.next();
                System.out.println("请重新输入确认密码:");
                passWord_ok = sc.next();
            } else {
                break;
            }
        }
        String carId = generateCarId(accounts);
        account.setCarId(carId);
        account.setUserName(userName);
        account.setPassWord(passWord);
        account.setQuotaMoney(2000);// 当日取钱额度
        accounts.add(account);
        System.out.println(account.getUserName() + "先生/女士开户成功，" + "新卡限额2000.0元，" + "您的卡号为:" + account.getCarId());

    }

    public static void login(ArrayList<Account> accounts, Scanner sc) {
        if (accounts.size() == 0) {
            System.out.println("系统没有账户，请先创建账户");
            return;
        }
        //有账户，就开始登录
        OUT:
        while (true) {
            System.out.println("ATM系统登录页面");
            System.out.println("1.输入卡号登录");
            System.out.println("2.退出登录页面，返回主页");
            System.out.println("输入命令操作");
            int operation = sc.nextInt();
            switch (operation) {
                case 1:
                    while (true) {
                        System.out.println("请输入卡号:");
                        String carId = sc.next();
                        if (carId.equals("退出")) {
                            break;
                        }
                        Account account = searchAccount(accounts, carId);
                        if (account == null) {
                            System.out.println("account不存在，请重新输入或者输入退出");
                        } else {
                            while (true) {
                                System.out.println("请输入密码:");
                                String passWord = sc.next();
                                if ("退出".equals(passWord)) {
                                    break;
                                } else if (account.getPassWord().equals(passWord)) {
                                    showUserCommand(sc, account,accounts);
                                    break OUT;
                                } else {
                                    System.out.println("密码错误,请重新输入或退出");
                                }
                            }
                            break;
                        }
                    }
                    break;
                case 2:
                    break OUT;
                default:
                    System.out.println("输入命令有误,请重新输入!");
            }
        }

    }

    public static void showUserCommand(Scanner sc, Account account, ArrayList<Account> accounts) {
        while (true) {
            System.out.println("=======欢迎用户操作页=======");
            System.out.println("1.查询当前登录账户信息");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            System.out.println("请输入命令:");
            int operation = sc.nextInt();
            switch (operation) {
                case 1:
                    //查询
                    System.out.println("用户名:" + account.getUserName());
                    System.out.println("余额:" + account.getMoney());
                    System.out.println("当日取钱限额:" + account.getQuotaMoney());
                    break;
                case 2:
                    //存款
                    saveMoney(account, sc);
                    break;
                case 3:
                    //取款
                    withDraw(account, sc);
                    break;
                case 4:
                    //转账
                    transferMoney(account,accounts,sc);
                    break;
                case 5:
                    //修改密码
                    updatePassword(account,sc);
                    return;
                case 6:
                    //退出
                    return;
                case 7:
                    //注销账户
                    accounts.remove(account);
                    return;
                default:
                    System.out.println("命令输入有误,重新输入！");
            }
        }
    }

    private static void updatePassword(Account account, Scanner sc) {
        while (true) {
            System.out.println("输入旧密码:");
            String oldPassword = sc.next();
            if (account.getPassWord().equals(oldPassword)){
                while (true) {
                    System.out.println("输入新密码");
                    String newPassword = sc.next();
                    System.out.println("再次输入新密码");
                    String newPassword_Ok = sc.next();
                    if (newPassword.equals(newPassword_Ok)){
                        account.setPassWord(newPassword);
                        System.out.println("密码更新成功！");
                        return;
                    }else{
                        System.out.println("新密码不一致，请重新输入");
                    }
                }
            }else{
                System.out.println("旧密码不正确，请重新输入!");
            }

        }
    }

    private static void withDraw(Account account, Scanner sc) {
        while (true) {
            System.out.println("请输入取款金额:");
            double money = sc.nextInt();
            double originalMoney = account.getMoney();
            if (originalMoney < money) {
                System.out.println("钱不够了！请重新输入取款金额");
            } else if (money > account.getQuotaMoney()) {
                System.out.println("取款限额2000元！请重新输入取款金额");
            } else {
                double currentMoney = originalMoney - money;
                account.setMoney(currentMoney);
                System.out.println("当前金额为:" + currentMoney);
                return;
            }
        }
    }

    public static void saveMoney(Account account, Scanner sc) {
        System.out.println("请输入存款金额:");
        int money = sc.nextInt();
        double currentMoney = account.getMoney() + money;
        account.setMoney(currentMoney);
        System.out.println("当前金额为:" + currentMoney);
    }
    public static  void transferMoney(Account account, ArrayList<Account> accounts,Scanner sc){
        if (accounts.size() < 2){
            System.out.println("当前系统账户小于2，不能转账。");
            return;
        }
        System.out.println("请输入对方账号:");
        String otherCardId = sc.next();
        Account otherAccount = searchAccount(accounts, otherCardId);
        if (otherAccount == null) {
            System.out.println("账户不存在");
            return;
        }
        System.out.println("请输入转账金额:");
        double transMoney = sc.nextDouble();
        if (transMoney > account.getMoney()){
            System.out.println("金额不够");
        }else if (transMoney>account.getQuotaMoney()){
            System.out.println("限额2000元");
        }else {
            double otherAccountMoney = otherAccount.getMoney();
            otherAccountMoney+= transMoney;
            otherAccount.setMoney(otherAccountMoney);
            double accountMoney = account.getMoney();
            accountMoney -= transMoney;
            account.setMoney(accountMoney);
            System.out.println("转账成功!");
            System.out.println("您的余额:"+account.getMoney());
            return;
        }
    }

}
